//
//  SYLetterKeyboard.m
//  SYKeyboard
//
//  Created by hc on 2019/3/1.
//  Copyright © 2019 hc. All rights reserved.
//

#import "SYLetterKeyboard.h"

@interface SYLetterKeyboard ()

/** 大小写标记 **/
@property (nonatomic, assign) BOOL isUp;
/** 字母按钮数组 **/
@property (nonatomic, strong) NSMutableArray *letterArray;

@end

@implementation SYLetterKeyboard

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _letterArray = [NSMutableArray array];
        [self setupSubviews];
    }
    return self;
}

/**
 快速创建字母键盘
 
 @param delegate 代理对象
 @return 键盘对象
 */
+ (SYLetterKeyboard *)creatLetterKeyboardWithDelegateTarget:(id<SYBaseKeyboardDelegate>)delegate {
    SYLetterKeyboard * keyboard = [[SYLetterKeyboard alloc] initWithFrame:CGRectMake(0, 0, SYKeyboardScreenWidth, SYKeyboardHeight)];
    keyboard.delegate = delegate;
    return keyboard;
}

- (void)setupSubviews {
    int row = 4;
    int coloumn = 10;
    int width = 5;
    int height = 10;
    
    CGFloat keyWidth = (SYKeyboardScreenWidth - width*11) / coloumn;
    CGFloat keyHeight = (SYKeyboardHeight - height*5) / row;
    
    CGFloat height1 = height;
    CGFloat height2 = height + height + keyHeight;
    CGFloat height3 = height + (height + keyHeight)*2;
    CGFloat height4 = height + (height + keyHeight)*3;
    
    NSString * str = @"q w e r t y u i o p a s d f g h j k l z x c v b n m . . . . .";
    NSArray * letterArr = [str componentsSeparatedByString:@" "];
    
    for (int i  = 0; i < letterArr.count; i++) {
        UIButton * bt = [[UIButton alloc] initWithFrame:CGRectMake(i * (keyWidth+width) + width, height1, keyWidth, keyHeight)];
        bt.tag = i;
        [bt setBackgroundImage:[[UIImage imageNamed:@"zimubaikuang"] stretchableImageWithLeftCapWidth:10 topCapHeight:10] forState:UIControlStateNormal];
        [bt setTitle:letterArr[i] forState:UIControlStateNormal];
        [bt setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        bt.titleLabel.font = [UIFont systemFontOfSize:18];
        [self addSubview:bt];
        [bt addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i > 9) bt.frame = CGRectMake((i-10) * (keyWidth+width) + width + (keyWidth + width)/2, height2, keyWidth, keyHeight);
        
        if (i > 18) bt.frame = CGRectMake((i-19) * (keyWidth+width) + width + (keyWidth + width)/2*3, height3, keyWidth, keyHeight);
        
        switch (i) {
            case 26:
            {
                [bt setBackgroundImage:[UIImage imageNamed:@"daxiaoxie"] forState:UIControlStateNormal];
                [bt setTitle:@" " forState:UIControlStateNormal];
                bt.frame = CGRectMake(width, height3, keyWidth/2*3, keyHeight);
            }
                break;
            case 27:
            {
                [bt setBackgroundImage:nil forState:UIControlStateNormal];
                [bt setImage:[UIImage imageNamed:@"icon_del_black"] forState:UIControlStateNormal];
                [bt setTitle:@" " forState:UIControlStateNormal];
                bt.frame = CGRectMake(SYKeyboardScreenWidth - width - keyWidth/2*3, height3, keyWidth/2*3, keyHeight);
            }
                break;
            case 28:
            {
                [bt setTitle:@"123" forState:UIControlStateNormal];
                bt.frame = CGRectMake(width, height4, keyWidth*2, keyHeight);
            }
                break;
            case 29:
            {
                [bt setTitle:@"空格" forState:UIControlStateNormal];
                bt.frame = CGRectMake((width + keyWidth)*2, height4, SYKeyboardScreenWidth - (width*2 + keyWidth*2)*2, keyHeight);
            }
                break;
            case 30:
            {
                [bt setTitle:@"符" forState:UIControlStateNormal];
                bt.frame = CGRectMake(SYKeyboardScreenWidth - width - keyWidth*2, height4, keyWidth*2, keyHeight);
            }
                break;
            default:
            
                break;
        }
        
        //------  添加引用  ------//
        if (i < 26) [_letterArray addObject:bt];
    }
}

/**
 切换大小写
 
 @param isUp 是否切换大写
 */
- (void)switchUpLowCase:(BOOL)isUp{
    NSString *upStr = @"Q W E R T Y U I O P A S D F G H J K L Z X C V B N M";
    NSString *lowStr = @"q w e r t y u i o p a s d f g h j k l z x c v b n m";
    
    NSArray * letterArr = [NSArray arrayWithArray:isUp?[upStr componentsSeparatedByString:@" "]:[lowStr componentsSeparatedByString:@" "]];
    
    [_letterArray enumerateObjectsUsingBlock:^(UIButton *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj setTitle:letterArr[idx] forState:UIControlStateNormal];
    }];
    
}


#pragma mark - 按钮点击事件
- (void)buttonTap:(UIButton *)sender{
    switch (sender.tag) {
        case 26:
        {
            _isUp = !_isUp;
            [self switchUpLowCase:_isUp];
            
            [sender setBackgroundImage:[UIImage imageNamed:_isUp?@"daxiaoxie_seleted":@"daxiaoxie"] forState:UIControlStateNormal];
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@"大小写" keyType:UpLowSwitchKeyType keyboardType:LetterKeyboard];
            }
        }
            break;
        case 27:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@"删除" keyType:DeleteKeyType keyboardType:LetterKeyboard];
            }
        }
            break;
        case 28:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@"123" keyType:NumberSwitchKeyType keyboardType:LetterKeyboard];
            }
        }
            break;
        case 29:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@" " keyType:LetterKeyType keyboardType:LetterKeyboard];
            }
        }
            break;
        case 30:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@"符" keyType:SymbolSwitchKeyType keyboardType:LetterKeyboard];
            }
        }
            break;
            
        default:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:sender.titleLabel.text keyType:LetterKeyType keyboardType:LetterKeyboard];
            }
        }
            break;
    }

}

@end
